package com.looeyWei;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class HandDraw extends JPanel {

    /**
     * 存储所有的曲线
     */
    ArrayList<Curve> curves = new ArrayList<>();

    public HandDraw(){
        MyMouseListener l = new MyMouseListener();
        this.addMouseListener(l);
        this.addMouseMotionListener(l);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 控件的宽高
        int width = this.getWidth();
        int height = this.getHeight();
        // 填空颜色
        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);
        // 绘制边框
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, width - 1, height - 1);

        // 两个以上的点才绘制
        for (Curve curve : curves) {
            if(curve.points.size() >= 2){
                //
                g.setColor(Color.RED);
                g.setFont(new Font("宋体", Font.BOLD, 15));

                Point p0 = curve.points.get(0);
                for (int i = 1; i < curve.points.size(); i++) {
                    Point p1 = curve.points.get(i);
                    g.drawLine(p0.x, p0.y, p1.x, p1.y);
                    p0 = p1;
                }
            }
        }

    }
    private class MyMouseListener extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);

            Curve curve = new Curve();
            curves.add(curve);
            curve.points.add(e.getPoint());  // 记录鼠标的坐标
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);

        }

        /**
         * 鼠标拖拽
         * @param e
         */
        @Override
        public void mouseDragged(MouseEvent e) {
            super.mouseDragged(e);

            Curve curve = curves.get(curves.size()-1);
            curve.points.add(e.getPoint());
            repaint();
        }

        /**
         * 鼠标点击
         * @param e
         */
        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);

            // 右键
            if(e.getButton() == MouseEvent.BUTTON3){
                // 菜单
                showMenu(e);
            }
        }
    }

    /**
     * 显示右键菜单
     * @param e
     */
    private void showMenu(MouseEvent e) {
        JPopupMenu menu = new JPopupMenu();

        //
        ContextMenuListenner menuListenner = new ContextMenuListenner();

        //
        JMenuItem itemSave = new JMenuItem("另存为");
        menu.add(itemSave);
        itemSave.addActionListener(menuListenner);
        itemSave.setActionCommand("save as");   // 区分

        //
        JMenuItem itemClear = new JMenuItem("清空");
        itemClear.addActionListener(menuListenner);
        itemClear.setActionCommand("clear");
        menu.add(itemClear);

        // 显示
        menu.show(e.getComponent(), e.getX(), e.getY());
    }

    /**
     * 保存文件
     */
    private void save() {
        JFileChooser chooser = new JFileChooser();

        // FileNameExtensionFilter 文件名后缀过滤器
        FileNameExtensionFilter filter = new FileNameExtensionFilter("图片文件", "jpg");
        chooser.setFileFilter(filter);
        // 显示对话框
        int ret = chooser.showSaveDialog(this);
        // 用户选择的结果
        if(ret == JFileChooser.APPROVE_OPTION){
            File file = chooser.getSelectedFile();
            // 截屏
            snapshot(this, file);
        }

    }

    /**
     * 对控件截屏
     * @param comp  哪一个控件
     * @param file  保存到哪里, 必须为jpg, 或者 png
     */
    private void snapshot(Component comp, File file) {
        // 创建一个image
        int width = comp.getWidth();
        int height = comp.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);

        // 将控件打印到Image上
        Graphics g = image.getGraphics();
        comp.paint(g);

        // 将Image保存
        try {
            ImageIO.write(image, "JPEG", file);
            System.out.println("保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //
    private class Curve{
        private ArrayList<Point> points = new ArrayList<>();
    }

    /**
     * 右键事件
     */
    private class ContextMenuListenner implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if("save as".equals(cmd)){
                System.out.println("保存");
                save();
            }
            if("clear".equals(cmd)){
                System.out.println("清空");
                curves.clear();
                repaint();
            }
        }
    }
}
